//
//  CMDBUserManageTableViewCell.swift
//  CMDB
//
//  Created by 王磊 on 23/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBUserManageTableViewCell: UITableViewCell {
    
    private var iconImageView: UIImageView = UIImageView()
    
    private var cover: UIView = UIView()
    
    private var nameLabel: UILabel = UILabel()
    
    private var positionLabel: UILabel = UILabel()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configOwnSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension CMDBUserManageTableViewCell {
    
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        backgroundColor = UIColor.whiteColor()
        
        contentView.addSubview(iconImageView)
        
        contentView.addSubview(nameLabel)
        
        contentView.addSubview(positionLabel)
        
        contentView.addSubview(cover)
        
        iconImageView.layer.cornerRadius = 5
        
        iconImageView.layer.masksToBounds = true
        
        nameLabel.textColor = word87Color
        
        positionLabel.textColor = word38Color
        
        nameLabel.textAlignment = .Left
        
        positionLabel.textAlignment = .Left
        
        nameLabel.font = CMDB_15_FONT
        
        positionLabel.font = CMDB_13_FONT
        
        cover.backgroundColor = word87Color
        
        cover.alpha = 0.3
        
        contentView.sendSubviewToBack(cover)
        
        cover.layer.cornerRadius = 5
        
        cover.layer.masksToBounds = true
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let temp = height - 20
        
        iconImageView.snp_makeConstraints { (make) in
            
            make.left.equalTo(10)
            
            make.centerY.equalTo(0)
            
            make.width.equalTo(temp)
            
            make.height.equalTo(temp)
        }
        
        cover.snp_makeConstraints { (make) in
            
            make.left.equalTo(10)
            
            make.centerY.equalTo(0)
            
            make.width.equalTo(temp)
            
            make.height.equalTo(temp)
            
        }
        
        nameLabel.snp_makeConstraints { [weak self] (make) in
            
            make.bottom.equalTo(self!.iconImageView.snp_centerY)
            
            make.left.equalTo(self!.iconImageView.snp_right).offset(5)
        }
        positionLabel.snp_makeConstraints { [weak self] (make) in
            
            make.top.equalTo(self!.iconImageView.snp_centerY)
            
            make.left.equalTo(self!.nameLabel.snp_left)
            
        }
        
    }
}
extension CMDBUserManageTableViewCell {
    
    override func update(data: AnyObject?) {
        
        let user = data as! CMDBUserInfoBean
        
        iconImageView.setHeader(user.icon)
        
        if user.iscommit {
            
            contentView.sendSubviewToBack(cover)
        } else {
            
            contentView.bringSubviewToFront(cover)
        }
        
        nameLabel.text = user.name
        
        positionLabel.text = user.name
        
    }
}